package com.iwinding.ladygg.bluetooth;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.iwinding.ladygg.utils.AMMapHelper;
import com.iwinding.ladygg.utils.HexStringUtil;
import com.junkchen.blelib.BleService;

import java.util.UUID;


/**
 * Created by 82218 on 2017/6/25.
 */

public class ICaneBleRoutine implements BleService.OnDataAvailableListener, BluetoothApi.OnBlueListener {

    private static ICaneBleRoutine INSTANCE = new ICaneBleRoutine();

    private boolean handshake;
    private boolean press;
    private boolean alarm;
    private boolean tips;
    private int volume;

    public boolean isHandshake() {
        return handshake;
    }

    public void setHandshake(boolean handshake) {
        this.handshake = handshake;
    }

    public boolean isPress() {
        return press;
    }

    public void setPress(boolean press) {
        this.press = press;
    }

    public boolean isAlarm() {
        return alarm;
    }

    public void setAlarm(boolean alarm) {
        this.alarm = alarm;
    }

    public boolean isTips() {
        return tips;
    }

    public void setTips(boolean tips) {
        this.tips = tips;
    }

    public int getVolume() {
        return volume;
    }

    public void setVolume(int volume) {
        this.volume = volume;
    }

    private Context mContext;

    public ICaneBleRoutine() {
        BluetoothApi.getInstance().setmOnBlueListener(this);
    }

    public static ICaneBleRoutine getInstance() {
        return INSTANCE;
    }

    public void initContext(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        //处理通知返回的数据
        byte[] value = characteristic.getValue();
        Log.i("@hzy", "onCharacteristicChanged-----" + HexStringUtil.bytesToHexString(value));
        BluetoothApi.getInstance().analysis(value);
    }

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

    }

    public void setICaneConnBluetoothGatt() {
        BluetoothGatt gatt = BluetoothManager.getInstance().getConnBluetoothGatt();
        if (gatt == null) {
            return;
        }

//        List<BluetoothGattService> gattServiceList = gatt.getServices();
//        BluetoothGattCharacteristic characteristic = null;
//        for (BluetoothGattService bluetoothGattService : gattServiceList) {
//            String uuid = bluetoothGattService.getUuid().toString().toUpperCase();
//            Log.i("@hzy","------service--="+uuid);
//            if (uuid.equals(MyGattAttributes.SERVICE_NAME)) {
//                characteristic = bluetoothGattService.getCharacteristic(UUID.fromString(MyGattAttributes.SERVICE_FEATURES));
//                BluetoothManager.getInstance().setCharacteristicNotification(characteristic, true);
//            }
//        }
//        if (characteristic != null){
//            BluetoothManager.getInstance().writeData(MyGattAttributes.SERVICE_NAME, MyGattAttributes.SERVICE_FEATURES, BluetoothApi.getInstance().cmdHandshake());
//            Log.i("@hzy","------characteristic--设置成功");
//        }

        BluetoothGattService iCaneService = gatt.getService(UUID.fromString(MyGattAttributes.SERVICE_NAME));
        if (iCaneService != null) {
            BluetoothGattCharacteristic characteristic = iCaneService.getCharacteristic(UUID.fromString(MyGattAttributes.SERVICE_FEATURES));
            if (characteristic != null) {
                BluetoothManager.getInstance().setCharacteristicNotification(characteristic, true);
                BluetoothManager.getInstance().writeData(MyGattAttributes.SERVICE_NAME, MyGattAttributes.SERVICE_FEATURES, BluetoothApi.getInstance().cmdHandshake());
            }
        }
    }

    @Override
    public void onHandshakeStatus(boolean handshake, boolean press, boolean alarm, boolean tips, int volume) {
        Log.i("@hzy","handshake------------"+handshake);
        Log.i("@hzy","press------------"+press);
        Log.i("@hzy","alarm------------"+alarm);
        Log.i("@hzy","tips------------"+tips);
        Log.i("@hzy","volume------------"+volume);
        this.handshake = handshake;
        this.press = press;
        this.alarm = alarm;
        this.tips = tips;
        this.volume = volume;
        BluetoothApi.getInstance().setVolume(volume);
        onDirection(alarm);
        onPress(press);
    }

    @Override
    public void onDirection(boolean alarm) {
        if (!handshake) {
            return;
        }
        if (alarm) {
            AMapLocation locationInfo = AMMapHelper.getInstance().getmLocationInfo();
            TMApi.getInstance().sendSOS(locationInfo);
        }
    }

    @Override
    public void onPress(boolean press) {
        if (!handshake) {
            return;
        }
        if (press) {
            AMapLocation locationInfo = AMMapHelper.getInstance().getmLocationInfo();
            TMApi.getInstance().sendSOS(locationInfo);
        }
    }

    @Override
    public void onVolume(int volume) {
        setVolume(volume);
    }


}
